import { reqCategoryList, reqBannerList, reqFloorList } from "@/api";
// 设置状态state
const state = {
  categoryList: [], // 三级分类
  bannerList: [],
  floorList: [],
};

// 获取内容getters
const getters = {
  // banners(state) {
  //   // HOF 高阶函数里返回值类型是函数
  //   return function (n) {
  //     return state.bannerList.slice(0, n);
  //   };
  // },
  banners(state) {
    return state.bannerList;
  },
};

// 修改数据 mutations
const mutations = {
  RECEIVE_CATEGORY_LIST(state, categoryList) {
    state.categoryList = categoryList;
  },
  RECEIVE_BANNER_LIST(state, bannerList) {
    state.bannerList = bannerList;
  },
  RECEIVE_FLOOR_LIST(state, floorList) {
    state.floorList = floorList;
  },
};

// 异步操作 actions
const actions = {
  // 获取三级分类
  async getCategoryList({ commit }) {
    const result = await reqCategoryList();
    if (result.code === 200) {
      commit("RECEIVE_CATEGORY_LIST", result.data);
    }
  },
  // 获取轮播图
  async getBannerList({ commit }) {
    // return new Promise(async (resolve, reject) => {
    //   const result = await reqBannerList();
    //   if (result.code === 200) {
    //     setTimeout(() => {
    //       commit("RECEIVE_BANNER_LIST", result.data);
    //       resolve(result.data);
    //     }, 100);
    //   }
    // });

    const result = await reqBannerList();
    if (result.code === 200) {
      setTimeout(() => {
        commit("RECEIVE_BANNER_LIST", result.data);
      }, 200);
    }
  },
  // 获取楼层数据
  async getFloorList({ commit }) {
    const result = await reqFloorList();

    if (result.code === 200) {
      commit("RECEIVE_FLOOR_LIST", result.data);
    }
  },
};

export default {
  state,
  getters,
  mutations,
  actions,
};
